package com.walker.jdkapi.thread.pool;

import java.util.concurrent.LinkedBlockingQueue;
import java.util.concurrent.TimeUnit;
import java.util.concurrent.atomic.AtomicInteger;

public class Provider implements Runnable {
	// 生产商品池
	private LinkedBlockingQueue<Noodle> lbq;

	// 计算器
	private static AtomicInteger count = new AtomicInteger();
	
	private volatile boolean isRunning = true;

	public Provider(LinkedBlockingQueue<Noodle> lbq) {
		super();
		this.lbq = lbq;
	}

	@Override
	public void run() {
		while(isRunning) {
			try {
				TimeUnit.MILLISECONDS.sleep(200);
			} catch (InterruptedException e1) {
				e1.printStackTrace();
			}
			int id = count.getAndIncrement();
			Noodle noodle = new Noodle();
			noodle.setId(id);
			noodle.setName("油泼面" + id);
			try {
				if (!lbq.offer(noodle, 2, TimeUnit.SECONDS)) {
					System.out.println("数据池添加数据失败：" + noodle.getName());
				}
			} catch (InterruptedException e) {
				e.printStackTrace();
			}
		}
	}
	
	/**
	 * 停止生产
	 */
	public void stop() {
		this.isRunning = false;
	}

}
